<?php

namespace App\Http\Controllers\V1\Admin;

use App\Models\V1\Config;
use Illuminate\Http\Request;

class ConfigController extends BaseController
{

    /**
     * 列表数据
     * @param Request $request
     * @return void
     */
    public function index(Request $request){
        $pageSize=$request->input('pageSize',10);
        $order=$request->input('order','desc');
        $sort=$request->input('sort','id');


        $query=Config::query();
        if($request->filled('config_name')){
            $query->where('config_name','like',"%".$request->input('config_name')."%");
        }
        if($request->filled('config_key')){
            $query->where('config_key','like',"%".$request->input('config_key')."%");
        }
        if($request->filled('config_type')){
            $query->where('config_type',$request->input('config_type'));
        }
        if($request->filled('begin_at')){
            $query->where('created_at',">=",$request->input('begin_at'));
        }
        if($request->filled('end_at')){
            $query->where('created_at',"<=",$request->input('end_at'));
        }
        $query->orderBy($sort,$order);
        $data=$query->paginate($pageSize);
        return ajax_output(200,"成功",$data);
    }

    /**
     * 添加配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request){
        $config_model=new Config();
        $config_model->create([
            'config_name'=>$request->input('config_name'),
            'config_key'=>$request->input('config_key'),
            'config_value'=>$request->input('config_value'),
            'config_type'=>$request->input('config_type'),
            'remark'=>$request->input('remark'),
        ]);
        return ajax_output(200,"成功");
    }

    /**
     * 修改配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request,$id){
        $config_model=new Config();
        $vo=$config_model->find($id);
        $vo->update([
            'config_name'=>$request->input('config_name'),
            'config_key'=>$request->input('config_key'),
            'config_value'=>$request->input('config_value'),
            'config_type'=>$request->input('config_type'),
            'remark'=>$request->input('remark'),
        ]);
        return ajax_output(200,"成功");
    }

    /**
     * 配置详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(Request $request,$id){
        $vo=Config::find($id);
        return ajax_output(200,"成功",$vo);
    }

    /**
     * 删除
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(Request $request,$id){
        Config::destroy($id);
        return ajax_output(200,"成功");
    }
}
